package sunwin.yoms.service.publicty;

import com.github.pagehelper.PageHelper;
import jodd.datetime.JDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.comm.YomsPage;
import sunwin.yoms.dao.NewsDao;
import sunwin.yoms.dao.VideoDao;
import sunwin.yoms.domain.publicty.NewsDomain;
import sunwin.yoms.domain.publicty.VideoDomain;
import sunwin.yoms.service.comm.YomsDefaultData;

import java.util.Date;
import java.util.List;

/**
 * Created by watson on 2015/7/27.
 */
@Service
public class NewsService {
    @Autowired
    private NewsDao newsDao;
    @Autowired
    private VideoDao videoDao;

    /**
     * 查询新闻
     *
     * @param newsDomain
     * @param yomsPage
     * @param startTime
     * @param endTime
     * @return
     * @throws YomsException
     */
    public List<NewsDomain> findNews(NewsDomain newsDomain, YomsPage yomsPage, Date startTime, Date endTime) throws YomsException {
        com.github.pagehelper.Page total = PageHelper.startPage(yomsPage.getPageIndex(), yomsPage.getPageSize(), true);
        List<NewsDomain> newsDomains = newsDao.getDomainCollectionExtend(newsDomain, startTime, endTime);
        yomsPage.setPageCount(total.getTotal() % yomsPage.getPageSize() == 0 ? total.getTotal() / yomsPage.getPageSize() : total.getTotal() / yomsPage.getPageSize() + 1);
        if (newsDomains.size() == 0) {
            throw new YomsException("没有找到新闻", 1);
        }
        return newsDomains;
    }

    /**
     * 增加新闻
     *
     * @param newsDomain
     * @throws YomsException
     */
    public void createNews(NewsDomain newsDomain) throws YomsException {
        newsDomain.setNewsCreateTime(new JDateTime(System.currentTimeMillis()).convertToDate());
        newsDomain.setNewsVerifyStateId(YomsDefaultData.VerifyStateId);
        NewsDomain news = new NewsDomain();
        news.setNewsTitle(newsDomain.getNewsTitle());
        List<NewsDomain> newsDomains = newsDao.getDomainByCondition(news);
        if (newsDomain.getNewsOriginSite() == null) {
            newsDomain.setNewsOriginSite(null);
        } else {
            if (newsDomain.getNewsOriginSite().contains("http://")) {
                newsDomain.setNewsOriginSite(newsDomain.getNewsOriginSite());
            } else {
                newsDomain.setNewsOriginSite("http://" + newsDomain.getNewsOriginSite());
            }
        }
        if (newsDomains.size() > 0) {
            throw new YomsException("该新闻已存在");
        } else {
            int result = newsDao.addDomain(newsDomain);
            if (result != 1) {
                throw new YomsException("创建新闻失败");
            }
        }
    }

    /**
     * 更新新闻
     *
     * @param newsDomain
     * @throws YomsException
     */
    public void updateNews(NewsDomain newsDomain) throws YomsException {
        NewsDomain domain = newsDao.getDomainById(newsDomain.getNewsId());
        if (newsDomain.getNewsOriginSite() == null) {
            newsDomain.setNewsOriginSite(null);
        } else {
            if (newsDomain.getNewsOriginSite().contains("http://")) {
                newsDomain.setNewsOriginSite(newsDomain.getNewsOriginSite());
            } else {
                newsDomain.setNewsOriginSite("http://" + newsDomain.getNewsOriginSite());
            }
        }
        if (domain == null) {
            throw new YomsException("非法请求");
        } else {
            if (newsDao.updateDomain(newsDomain) != 1) {
                throw new YomsException("更新新闻失败");
            }
        }
    }

    /**
     * 删除新闻
     *
     * @param id
     * @throws YomsException
     */
    public void delNews(int id) throws YomsException {
        NewsDomain domain = newsDao.getDomainById(id);
        if (domain == null) {
            throw new YomsException("非法请求");
        } else {
            if (newsDao.deleteDomain(domain) != 1) {
                throw new YomsException("删除新闻失败");
            }
        }
    }

    /**
     * 通过ID查找新闻
     *
     * @param id
     * @return
     * @throws YomsException
     */
    public NewsDomain getNewsById(int id) throws YomsException {
        NewsDomain domain = newsDao.getDomainById(id);
        VideoDomain videoDomain = domain.getVideoDomain();
        String imgPath = "video/img/" + videoDomain.getVideoFileName() + ".jpg";
        videoDomain.setVideoImage(imgPath);

        if (domain == null) {
            throw new YomsException("无法找到指定新闻", 1);
        } else {
            if (domain.getVideoDomain().getVideoId() == 1) {
                domain.getVideoDomain().setVideoName(null);
            }
        }
        return domain;
    }

    /**
     * 查询新闻总数量
     *
     * @return
     */
    public int getNewsCount() {
        return newsDao.getDomainCount();
    }

    /**
     * 给新闻热度+1
     *
     * @param nid
     */
    public void updateNewsHot(int nid) {
        NewsDomain domain = newsDao.getDomainById(nid);
        if (domain != null) {
            long hot = domain.getNewsHot();

            if (hot < Long.MAX_VALUE) {
                NewsDomain newsHot = new NewsDomain();
                newsHot.setNewsId(domain.getNewsId());
                newsHot.setNewsHot(hot + 1);

                newsDao.updateDomain(newsHot);
            }

        }
    }

    /**
     * 根据热度获取新闻
     *
     * @return
     */
    public List<NewsDomain> getNewsByHot(int pageSize) {
        YomsPage yomsPage = new YomsPage();
        yomsPage.setPageSize(pageSize);

        PageHelper.startPage(yomsPage.getPageIndex(), yomsPage.getPageSize(), true);
        PageHelper.orderBy("news_hot desc");
        List<NewsDomain> newsDomains = newsDao.getDomainCollectionExtend(null, null, null);

        return newsDomains;
    }


    /**
     * 通过TypeId查询公园快讯
     */
    public List<NewsDomain> getNewsByTypeId(int newsTypeId, YomsPage yomsPage) {
        NewsDomain news = new NewsDomain();
        news.setNewsTypeId(newsTypeId);

        com.github.pagehelper.Page total = PageHelper.startPage(yomsPage.getPageIndex(), yomsPage.getPageSize(), true);
        List<NewsDomain> newsDomains = newsDao.getDomainCollectionExtend(news, null, null);
        yomsPage.setPageCount(total.getTotal() % yomsPage.getPageSize() == 0 ? total.getTotal() / yomsPage.getPageSize() : total.getTotal() / yomsPage.getPageSize() + 1);

        return newsDomains;
    }

    /**
     * 通过TypeId查询公园快讯
     */
    public List<NewsDomain> getNewsByTypeId(int tid, int pageSize) throws YomsException {
        NewsDomain news = new NewsDomain();
        news.setNewsTypeId(tid);
        news.setNewsImg("img");

        PageHelper.startPage(1, pageSize, true);
        PageHelper.orderBy("news_create_time desc");
        List<NewsDomain> newsDomains = newsDao.getDomainCollectionExtend(news, null, null);

        return newsDomains;
    }

    /**
     * 查找最新添加带缩略图的公告
     *
     * @return
     */
    public NewsDomain getUptodateNotice() {
        return getSingleNewsByType(2);
    }

    /**
     * 查找最新添加带缩略图的快讯
     *
     * @return
     */
    public NewsDomain getUptodateFlash() {
        return getSingleNewsByType(1);
    }

    /**
     * 查找最新添加带缩略图的赛事
     *
     * @return
     */
    public NewsDomain getUptodateEvents() {
        return getSingleNewsByType(3);
    }

    /**
     * 获取某一天的赛事
     *
     * @return
     */
    public List<NewsDomain> getTodayEvents(JDateTime dateTime, int pageSize) {
        return getCustomizeDate(dateTime.convertToDate(), dateTime.addDay(1).convertToDate(), pageSize, 3);
    }

    /**
     * 获取某一天之后的赛事
     *
     * @return
     */
    public List<NewsDomain> getAfterEvents(JDateTime dateTime, int pageSize) {
        return getCustomizeDate(dateTime.addDay(1).convertToDate(), dateTime.addYear(1000).convertToDate(), pageSize, 3);

    }

    /**
     * 获取健身培训
     *
     * @param pageSize
     * @return
     */
    public List<NewsDomain> getSportCultivate(int pageSize) {
        return getUptodateSports(5, pageSize);
    }

    /**
     * 获取健身指南
     *
     * @param pageSize
     * @return
     */
    public List<NewsDomain> getSportFingerpost(int pageSize) {
        return getUptodateSports(4, pageSize);
    }

    /**
     * 根据新闻类型查找
     *
     * @param pageSize
     * @param typeId
     * @return
     */
    private List<NewsDomain> getUptodateSports(int typeId, int pageSize) {
        YomsPage yomsPage = new YomsPage();
        yomsPage.setPageSize(pageSize);

        NewsDomain newsDomain = new NewsDomain();
        newsDomain.setNewsImg("img");
        newsDomain.setNewsTypeId(typeId);

        PageHelper.startPage(yomsPage.getPageIndex(), yomsPage.getPageSize(), true);
        PageHelper.orderBy("news_create_time desc");
        List<NewsDomain> newsDomains = newsDao.getDomainCollectionExtend(newsDomain, null, null);

        return newsDomains;
    }

    /**
     * 获取指定日期的新闻
     *
     * @param startTime 起始时间
     * @param endTime   截至时间
     * @param pageSize  需要多少条数据
     * @param tid       新闻类型ID
     * @return
     */
    private List<NewsDomain> getCustomizeDate(Date startTime, Date endTime, int pageSize, int tid) {
        YomsPage yomsPage = new YomsPage();
        yomsPage.setPageSize(pageSize);

        NewsDomain domain = new NewsDomain();
        domain.setNewsTypeId(tid);

        PageHelper.startPage(yomsPage.getPageIndex(), yomsPage.getPageSize(), true);
        PageHelper.orderBy("news_create_time desc");
        List<NewsDomain> domains = newsDao.getSportsCollectionExtend(domain, startTime, endTime);

        return domains;
    }

    /**
     * 根据新闻类型查找单个包含缩略图且最新的新闻
     *
     * @param newsType
     * @return
     */
    private NewsDomain getSingleNewsByType(int newsType) {
        YomsPage yomsPage = new YomsPage();
        yomsPage.setPageSize(1);

        NewsDomain newsDomain = new NewsDomain();
        newsDomain.setNewsImg("img");
        newsDomain.setNewsTypeId(newsType);

        PageHelper.startPage(yomsPage.getPageIndex(), yomsPage.getPageSize(), true);
        PageHelper.orderBy("news_create_time desc");
        List<NewsDomain> newsDomains = newsDao.getDomainCollectionExtend(newsDomain, null, null);

        if (newsDomains.size() > 0) {
            return newsDomains.get(0);
        } else {
            return null;
        }
    }
}
